﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using visinia.SmartEngine;
using visinia.SmartEngine.Installer;
using visinia.SmartEngine.Pages;

public partial class Admin_Controls_Pages : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LiteralControl pages = new LiteralControl(Render());
        Controls.Add(pages);
    }

    private string Render()
    {
        int currentPageId = 0;
        Int32.TryParse(Request.QueryString["PageId"], out currentPageId);
        string designerLink = "<a href=\"" + WebRoots.GetWebRoot + "Designer.aspx?PageId={0}\">";
        string designerLinkCurrentPage = "<a class=\"current-page\" href=\"" + WebRoots.GetWebRoot + "Designer.aspx?PageId={0}\">";
        StringBuilder sb = new StringBuilder();
        sb.Append("<ul class=\"nav\"><li class=\"nav-container\"><a class=\"nav-header\" >Pages</a>");
        sb.Append("<ul class=\"nav-main\">");
        foreach (Plugin plugin in PluginController.GetInstalledPlugins())
        {
            List<PageInfo> pages = PageController.GetPages(plugin.PluginId);
            if (pages.Count == 0)
                continue;
            sb.Append("<li><a>" + plugin.Name + "</a>");
            sb.Append("<ul class=\"nav-sub\" >");
            foreach (PageInfo page in pages)
            {
                sb.Append("<li>");   
                    if(page.PageId == currentPageId)
                        sb.Append(string.Format(designerLinkCurrentPage, page.PageId));
                    else
                        sb.Append(string.Format(designerLink, page.PageId));
                    sb.Append(page.Name);
                sb.Append("</a>");               
                sb.Append("</li>");
            }
            sb.Append("</ul>");
            sb.Append("</li>");
        }
        sb.Append("</ul>");
        sb.Append("</li></ul>");
        return sb.ToString();
    }   
}
